/*
 * xAPdrv.java
 *
 * xAP driver for MyOpenLab
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 13/08/09 by Daniel Berenguer : first version
 */

import VisualLogic.*;
import xAP.xAPthread;
import xAP.drvCore;
import xAP.xAPelement;
import xAP.xAPtypes.TypeOfElement;

import java.util.*;

import java.util.Vector;
import xAP.configParser;
import xAP.xAPBSCendpoint;
import xAP.xAPBSCstate;
import xAP.xAPBSCtext;


/********************************************************************************************
 * xAPdrv class
 *
 * xAP driver class
 *
 *********************************************************************************************/
public class xAPdrv implements MyOpenLabDriverIF
{
    private xAPthread xAPserver;
    private xAPelement xElement;

   /********************************************************************************************
    * xAPdrv
    *
    * Class constructor
    *
    ********************************************************************************************/
    public xAPdrv()
    {
        // Read general config
        configParser.readGeneral("xAP.xml");
        
        // Run xAP server
        xAPserver = new xAPthread();
        xAPserver.start();

        // Create vector of VM xAP elements
        drvCore.vXAPelements = new Vector();
    }

    @Override
    public void driverStart(ArrayList args)
    {
        xElement = new xAPelement();
        xElement.setSource((String)args.get(0));
        xElement.setType((TypeOfElement)args.get(1));
    }

   /********************************************************************************************
    * sendCommand
    *
    * Command received from x VM xAP element
    *
    ********************************************************************************************/
    @Override
    public void sendCommand(String commando, Object value)
    {
        if (commando.equals(drvCore.DRVCOMM_REQU_BSCVAL))
        {
            String SourceAddr = value.toString();
            // Get VM element
            xAPelement VMElement = drvCore.getFirstElementFromSource(SourceAddr);

            while (VMElement != null)
            {
                // Find BSC endpoint
                xAPBSCendpoint endpoint = drvCore.bscEndpoints.getEndpointFromSource(SourceAddr);

                // Type of BSC element
                TypeOfElement type = xElement.getType();
                // Send new value to the VM element, depending on the expected data type
                switch(type)
                {
                    case BSC_INPUT_STA:
                        if (endpoint == null)
                            VMElement.SendValue(new xAPBSCstate("OFF"));
                        else
                            VMElement.SendValue(endpoint.state);
                        break;
                    case BSC_INPUT_NUM:
                        if (endpoint == null)
                            VMElement.SendValue(new xAPBSCtext("0"));
                        else if (endpoint.level == null)
                            VMElement.SendValue(endpoint.text);
                        else
                            VMElement.SendValue(endpoint.level);
                        break;
                    default:
                        break;
                }

                VMElement = drvCore.getNextElementFromSource(SourceAddr);
            }
        }
    }

   /********************************************************************************************
    * registerOwner
    *
    * xAP element tries to register itself into the xAP server
    *
    ********************************************************************************************/    
    @Override
    public void registerOwner(MyOpenLabDriverOwnerIF owner)
    {
        xElement.setOwner(owner);
        xElement.AddToTable();
    }

   /********************************************************************************************
    * driverStop
    *
    * xAP driver stopped
    *
    ********************************************************************************************/
    @Override
    public void driverStop()
    {
        //System.out.println("xAP driver stopped");
    }
}
